package com.survey.service.impl;

import com.github.pagehelper.PageHelper;
import com.survey.dao.mapper.QuestionMapper;
import com.survey.dao.mapper.ResultMapper;
import com.survey.dao.mapper.SurveyMapper;
import com.survey.pojo.data.*;
import com.survey.pojo.vo.ResultVO;
import com.survey.service.SurveyService;
import com.survey.utils.PageBean;
import com.survey.utils.Result;
import com.survey.pojo.vo.SurveyVO;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <br/>
 * Created by survey on 2018/08/03
 */
@Service("surveyService")
public class SurveyServiceImpl implements SurveyService {

    private final static Logger LOG = LoggerFactory.getLogger(SurveyServiceImpl.class);

    @Resource
    private SurveyMapper surveyMapper;

    @Autowired
    QuestionMapper questionMapper;

    @Autowired
    ResultMapper resultMapper;

    @Override
    public String addSurvey(SurveyVO surveyVO) throws Exception {
        Integer addSurvey = surveyMapper.addSurvey(surveyVO);
        if (addSurvey>0){
            return "添加成功";
        }
        return "添加失败";
    }

    @Override
    public Result updateSurvey(SurveyVO surveyVO) throws Exception {
        Integer count = surveyMapper.updateSurvey(surveyVO);
        if(count>0){
            return Result.getSuccess("更改成功");
        }
        return Result.getFailure("更改失败");
    }

    @Override
    public SurveyDO findDetailSurvey(SurveyVO surveyVO) throws Exception{
        return surveyMapper.findDetailSurvey(surveyVO);
    }

    @Override
    public PageBean<SurveyVO> listSurvey(SurveyVO query) throws Exception {
        int count=surveyMapper.countSurvey(query);

        //计算最大页数
        int max = (count + query.getPageSize() - 1) / query.getPageSize();

        if (max == 0) {
            return null;
        }


        if (query.getPageCode() > max) {
//            throw new PageCodeException("页码越界啦", 1);
        }
        PageHelper.startPage(query.getPageCode(), query.getPageSize());

        List<SurveyDO> surveyDOS = surveyMapper.listSurvey(query);
        ArrayList surveyVOS = new ArrayList();
        //门面值的转换
        for (SurveyDO surveyDO:surveyDOS){
            SurveyVO surveyVO=new SurveyVO();
            BeanUtils.copyProperties(surveyVO,surveyDO);
            surveyVOS.add(surveyVO);
        }

        PageBean<SurveyVO> pageBean = new PageBean<SurveyVO>(surveyVOS, query.getPageSize()
                , query.getPageCode(), count, query.getSize());
        return pageBean;
    }


    @Override
    public List<SurveyDO> listSurvey() {
        List<SurveyDO> surveyDOS = surveyMapper.listBytime();
        return surveyDOS;
    }

    @Override
    public List<SurveyVO> listAllSurvey() {
        return null;
    }

    @Override
    public PageBean<SurveyDO> listSurveyisopen1(SurveyVO surveyVO) throws Exception {
        //防止页码越界
        Integer count = surveyMapper.countStudent(surveyVO);
        int max = (count +surveyVO.getPageSize() - 1) / surveyVO.getPageSize();
        if (surveyVO.getPageCode() > max) {
            surveyVO.setPageCode(max);
        }
        PageHelper.startPage(surveyVO.getPageCode(), surveyVO.getPageSize());
        List<SurveyDO> surveyDOS = surveyMapper.listSurvey(surveyVO);
        PageBean<SurveyDO> pageBean = new PageBean<SurveyDO>(surveyDOS, surveyVO.getPageSize()
                , surveyVO.getPageCode(),count, surveyVO.getSize());
        return pageBean;
    }

    @Override
    public SurveyDO find(String name) {
        return  surveyMapper.find(name);
    }

    @Override
    public QuestionBackVO back(Integer sid) {
        QuestionBackVO vo = new QuestionBackVO();
        List<QuestionDataVO> bysurvey = questionMapper.findBysurvey(sid);
        for (QuestionDataVO qvo:bysurvey) {
            if(qvo.getType()==1){
                vo.addSingleQuestion(qvo);
            }else {
                vo.addMultiQuestion(qvo);
            }
        }
        SurveyVO surveyVO = new SurveyVO();
        surveyVO.setId(sid);
        SurveyDO survey = surveyMapper.findDetailSurvey(surveyVO);
        vo.setName(survey.getName());
        vo.setDesc(survey.getDesc());
        vo.setImg(survey.getImg());
        return vo;
    }

    @Override
    public Result listSurveyPage(SurveyVO surveyVO) throws Exception{
        return null;
    }

    @Override
    public Result countSurvey(SurveyVO surveyVO) throws Exception{
        return null;
    }

    @Override
    public Result deleteSurvey(SurveyVO surveyVO) throws Exception{
        return null;
    }

    public Result submit(ExamAnswerVO answerVO ){
        List<ExamAnswerQuestions> questions = answerVO.getQuestions();
        for (ExamAnswerQuestions eq:questions) {
            ResultVO resultVO = new ResultVO();
            resultVO.setFkSid(Integer.parseInt(answerVO.getEid()));
            resultVO.setFkQid(Integer.parseInt(eq.getId()));
            ResultDO resultDO = resultMapper.findResultByVO(resultVO);
            if(resultDO==null){
                //表示没有 要插入
                resultVO.setCounta(0);
                resultVO.setCountb(0);
                resultVO.setCountc(0);
                resultVO.setCountd(0);
                if(eq.getType()==1){
                    if(eq.getAnswer().equals("0")){
                        resultVO.setCounta(1);
                    }
                    if(eq.getAnswer().equals("1")){
                        resultVO.setCountb(1);
                    }
                    if(eq.getAnswer().equals("2")){
                        resultVO.setCountc(1);
                    }
                    if(eq.getAnswer().equals("3")){
                        resultVO.setCountd(1);
                    }
                    resultMapper.addResult(resultVO);
                }else {
                    String[] split = eq.getAnswer().split(",");
                    for (String s:split) {
                        if(s.equals("0")){
                            resultVO.setCounta(1);
                        }
                        if(s.equals("1")){
                            resultVO.setCountb(1);
                        }
                        if(s.equals("2")){
                            resultVO.setCountc(1);
                        }
                        if(s.equals("3")){
                            resultVO.setCountd(1);
                        }
                    }
                    resultMapper.addResult(resultVO);
                }
            }else {
                if(eq.getType()==1){
                    if(eq.getAnswer().equals("0")){
                        resultVO.setCounta(resultDO.getCounta()+1);
                    }
                    if(eq.getAnswer().equals("1")){
                        resultVO.setCountb(resultDO.getCountb()+1);
                    }
                    if(eq.getAnswer().equals("2")){
                        resultVO.setCountc(resultDO.getCountc()+1);
                    }
                    if(eq.getAnswer().equals("3")){
                        resultVO.setCountd(resultDO.getCountd()+1);
                    }
                    resultVO.setId(resultDO.getId());
                    resultMapper.updateResult(resultVO);
                }else {
                    String[] split = eq.getAnswer().split(",");
                    for (String s:split) {
                        if(s.equals("0")){
                            resultVO.setCounta(resultDO.getCounta()+1);
                        }
                        if(s.equals("1")){
                            resultVO.setCountb(resultDO.getCountb()+1);
                        }
                        if(s.equals("2")){
                            resultVO.setCountc(resultDO.getCountc()+1);
                        }
                        if(s.equals("3")){
                            resultVO.setCountd(resultDO.getCountd()+1);
                        }
                    }
                    resultVO.setId(resultDO.getId());
                    resultMapper.updateResult(resultVO);
                }
            }
        }
        SurveyVO surveyVO = new SurveyVO();
        surveyVO.setId(Integer.parseInt(answerVO.getEid()));
        SurveyDO detailSurvey = surveyMapper.findDetailSurvey(surveyVO);
        try {
            BeanUtils.copyProperties(surveyVO,detailSurvey);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        surveyVO.setCount(detailSurvey.getCount().intValue()+1);
        surveyMapper.updateSurvey(surveyVO);
        return new Result(Result.CODE_SUCCESS,"成功");
    }

    @Override
    public List<SurveyDO> listhotsurvey() {
        return surveyMapper.listhotsurvey();
    }

    @Override
    public SurveyDO privateSurveyShow(String sid) {
        return surveyMapper.privateSurveyShow(sid);
    }

}